package fzzl

import org.apache.commons.lang.StringEscapeUtils
import org.springframework.dao.DataIntegrityViolationException

import java.nio.ByteBuffer
import java.nio.charset.Charset
import java.util.regex.Matcher
import java.util.regex.Pattern

class DrugController {

    static allowedMethods = [save: "POST", update: "POST", delete: "POST"]

    def index() {
        redirect(action: "list", params: params)
    }

    def list(Integer max) {

        params.max = Math.min( 1,2)
        def list=Drug.list();
        def list1=Drug.list(params);
        for(int i=0;i<list.size();i++)
        {
            String fangjie=list.get(i).drugYj
            if(null!=fangjie&&""!=fangjie)
            {
                fangjie=URLDecoder.decode(fangjie);
                fangjie=fangjie.replaceAll("\\\\/","/")
                fangjie=fangjie.replaceAll("\\\\r","")
                fangjie=fangjie.replaceAll("\\\\n","")
                fangjie=fangjie.replaceAll("\\\\t","")
                fangjie=fangjie.replaceAll("=\\\\","=")
                fangjie=fangjie.replaceAll("\\\\\"","\"")
                fangjie=StringEscapeUtils.unescapeJava(fangjie);
                print(fangjie)

                list.get(i).drugYj=fangjie;
                list.get(i).save(flush: true)
            }
        }

        [drugInstanceList: list1, drugInstanceTotal: Drug.count()]
    }

    public static String decodeUnicode(String str) {
        Charset set = Charset.forName("UTF-8");
        Pattern p = Pattern.compile("\\\\u([0-9a-fA-F]{4})");
        Matcher m = p.matcher(str);
        int start = 0;
        int start2 = 0;
        StringBuffer sb = new StringBuffer();
        while (m.find(start)) {
            start2 = m.start();
            if (start2 > start) {
                String seg = str.substring(start, start2);
                sb.append(seg);
            }
            String code = m.group(1);
            int i = Integer.valueOf(code, 16);
            byte[] bb = new byte[4];
            bb[0] = (byte) ((i >> 8) & 0xFF);
            bb[1] = (byte) (i & 0xFF);
            ByteBuffer b = ByteBuffer.wrap(bb);
            sb.append(String.valueOf(set.decode(b)).trim());
            start = m.end();
        }
        start2 = str.length();
        if (start2 > start) {
            String seg = str.substring(start, start2);
            sb.append(seg);
        }
        return sb.toString();

    }




    def create() {
        [drugInstance: new Drug(params)]
    }

    def save() {
        def drugInstance = new Drug(params)
        if (!drugInstance.save(flush: true)) {
            render(view: "create", model: [drugInstance: drugInstance])
            return
        }

        flash.message = message(code: 'default.created.message', args: [message(code: 'drug.label', default: 'Drug'), drugInstance.id])
        redirect(action: "show", id: drugInstance.id)
    }

    def show(Long id) {
        def drugInstance = Drug.get(id)
        if (!drugInstance) {
            flash.message = message(code: 'default.not.found.message', args: [message(code: 'drug.label', default: 'Drug'), id])
            redirect(action: "list")
            return
        }

        [drugInstance: drugInstance]
    }

    def edit(Long id) {
        def drugInstance = Drug.get(id)
        if (!drugInstance) {
            flash.message = message(code: 'default.not.found.message', args: [message(code: 'drug.label', default: 'Drug'), id])
            redirect(action: "list")
            return
        }

        [drugInstance: drugInstance]
    }

    def update(Long id, Long version) {
        def drugInstance = Drug.get(id)
        if (!drugInstance) {
            flash.message = message(code: 'default.not.found.message', args: [message(code: 'drug.label', default: 'Drug'), id])
            redirect(action: "list")
            return
        }

        if (version != null) {
            if (drugInstance.version > version) {
                drugInstance.errors.rejectValue("version", "default.optimistic.locking.failure",
                          [message(code: 'drug.label', default: 'Drug')] as Object[],
                          "Another user has updated this Drug while you were editing")
                render(view: "edit", model: [drugInstance: drugInstance])
                return
            }
        }

        drugInstance.properties = params

        if (!drugInstance.save(flush: true)) {
            render(view: "edit", model: [drugInstance: drugInstance])
            return
        }

        flash.message = message(code: 'default.updated.message', args: [message(code: 'drug.label', default: 'Drug'), drugInstance.id])
        redirect(action: "show", id: drugInstance.id)
    }

    def delete(Long id) {
        def drugInstance = Drug.get(id)
        if (!drugInstance) {
            flash.message = message(code: 'default.not.found.message', args: [message(code: 'drug.label', default: 'Drug'), id])
            redirect(action: "list")
            return
        }

        try {
            drugInstance.delete(flush: true)
            flash.message = message(code: 'default.deleted.message', args: [message(code: 'drug.label', default: 'Drug'), id])
            redirect(action: "list")
        }
        catch (DataIntegrityViolationException e) {
            flash.message = message(code: 'default.not.deleted.message', args: [message(code: 'drug.label', default: 'Drug'), id])
            redirect(action: "show", id: id)
        }
    }
}
